package com.pb.camera.jph.activity;

import com.pb.camera.HomePageActivity;
import com.pb.camera.LogInActivity;
import com.pb.camera.R;
import com.pb.camera.broadcast.WifiBroadcastReceiver;
import com.pb.camera.broadcast.WifiSettingProxy;
import com.pb.camera.constants.MyContants;
import com.pb.camera.more.MoreActivity;
import com.pb.camera.utils.Logger;
import com.pb.camera.utils.TaskHelper;
import com.pb.camera.view.RoundProgressBar;
import com.pb.camera.work.ControlTask;
import com.pb.camera.work.IOTCInit;
import com.pb.camera.work.ReceiveTask;
import com.tutk.IOTC.AVIOCTRLDEFs;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.Toast;

import java.util.Observable;
import java.util.Observer;

public class ConnectCameraNetActivity extends Activity  {
    private static final String TAG = "ConnectCameraNetActivity";
    private ImageButton ib_back_add;
    private RoundProgressBar rpb_net_connect;
    private WifiAdmin mWifiAdmin;
    private boolean mIsSetting;
    private WifiSettingProxy mWifiProxy;
    private String uuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net_camera_connect);
        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        mWifiAdmin = new WifiAdmin(this);
        Bundle bundle = intent.getBundleExtra("wifiInfo");
        ScanResult wifiScan = bundle.getParcelable("wifiConfig");
        String wifiPassword = bundle.getString("wifiPassword");
        uuid = bundle.getString("UUID");
        connectWifi(wifiScan, wifiPassword);
    }

    private void connectWifi(final ScanResult wifiScan, final String wifiPassword) {
        rpb_net_connect.startProgress();
        mWifiProxy = new WifiSettingProxy(ConnectCameraNetActivity.this,wifiScan);
        Intent intent = new Intent(this,HomePageActivity.class);
        intent.putExtra("UUID",uuid);
        mWifiProxy.setIntent(intent);
        TaskHelper.exec(new Runnable() {
            @Override
            public void run() {
                mIsSetting = true;
                ControlTask.setWifi(0, wifiScan, wifiPassword);
                mWifiAdmin.connectWifi(wifiScan, MyContants.DEFAULT_AP_PASSWORD);
            }
        });
    }

    private void initView() {
        ib_back_add = (ImageButton) findViewById(R.id.ib_back_add);
        rpb_net_connect = (RoundProgressBar) findViewById(R.id.rpb_net_connect);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mIsSetting)
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWifiProxy != null)
            mWifiProxy.finishProxy();
    }
}
